<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加部门信息' : '编辑部门信息'" :visible.sync="addEditIsDialog_c"
      @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="公司名称：" prop="companyId">
              <el-cascader v-model="ruleForm.companyId" placeholder="请选择公司名称" :options="companyArray" :props="{
                checkStrictly: true,
                value: 'id',
                label: 'name',
                children: 'childList',
              }" @change="changeTreeItem" :disabled="seeType" clearable></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="24">
            <el-form-item label="上级部门：" prop="parentId">
              <el-cascader
                v-model="ruleForm.parentId"
                placeholder="请选择上级部门"
                :options="departArray"
                :props="{
                  checkStrictly: true,
                  value: 'id',
                  label: 'name',
                  children: 'childList',
                }"
                @change="changeTreeItemDepart"
                :disabled="seeType"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="24">
            <el-form-item label="基地编码：" prop="code">
              <el-input v-model="ruleForm.code" placeholder="请输入基地编码" clearable maxlength="50" show-word-limit
                :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="基地名称：" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入基地名称" clearable maxlength="50" show-word-limit
                :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button class="btn-light-blue" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import {
  getDepartInfoTree,
  getSysCompanyBase,
  saveSysCompanyBase,
  getCompanyInfoTree,
} from "@/api/company";

export default {
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        companyId: "",
        parentId: "",
        code: "",
        name: "",
      },
      rules: {
        // 表单规则
        companyId: [
          { required: true, message: "请选择公司名称", trigger: "change" },
        ],
        parentId: [
          { required: true, message: "请选择上级部门", trigger: "change" },
        ],
        code: [{ required: true, message: "请输入部门编码", trigger: "blur" }],
        name: [{ required: true, message: "请输入部门名称", trigger: "blur" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      departArray: [], // 部门集合
      companyArray: [], // 公司集合
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

        // 获取公司信息树形列表
        this.getCompanyInfoTree();
      }
    },
  },
  methods: {
    // 详情
    getDetail() {
      getSysCompanyBase({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data;

          // 获取部门信息树形列表
          // this.getDepartInfoTree();
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveSysCompanyBase(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    // 获取公司列表
    getCompanyInfoTree() {
      getCompanyInfoTree({}).then((res) => {
        if (res.code == 200) {
          this.companyArray = res.data;
        }
      });
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.ruleForm.companyId = val[val.length - 1];
      } else {
        this.ruleForm.companyId = "";
      }

      this.ruleForm.parentId = "";
      this.departArray = [];

      // 获取部门信息树形列表
      // this.getDepartInfoTree();
    },
    // 获取部门信息树形列表
    // getDepartInfoTree() {
    //   if (!this.ruleForm.companyId || this.ruleForm.companyId == "") {
    //     return;
    //   }
    //   getDepartInfoTree({ companyId: this.ruleForm.companyId }).then((res) => {
    //     if (res.code == 200) {
    //       this.departArray = res.data;
    //       this.departArray.unshift({ id: 0, name: "无" });
    //     }
    //   });
    // },
    // 部门下拉切换
    changeTreeItemDepart(val) {
      if (val) {
        this.ruleForm.parentId = val[val.length - 1];
      } else {
        this.ruleForm.parentId = "";
      }
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-cascader {
    width: 100%;
  }
}
</style>
